package com.lsm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ChangyongUtils {

    public static Map<String,Object> getZhi() throws Exception {
        // 获取当前目录的路径
        String currentDir = System.getProperty("user.dir");
        // 创建File对象，指向当前目录下的txt文件
        try  {
            Map<String,Object> map=new HashMap<>();
            File file = new File(currentDir + "/关键信息.txt"); // 确保文件名正确
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line!=null && !"".equals(line)){
                    String s = line.replaceAll("，", ",");
                    String[] split = s.split("=");
                    if (split.length>1){
                        map.put(split[0],split[1]);
                    }
                }
            }

            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;


    }

    public static String macCheck(){
        String macAddress = getMacAddress();
        if (macAddress==null){
            return null;
        }

        return httpClient(macAddress);
    }



    public static String httpClient(String macAddress){
        try {
            URL url = new URL("http://47.109.144.43:8100/maccheck?macAddress="+macAddress);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(20000);
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            return response.toString();
        } catch (Exception e) {

        }
        return null;
    }



    public static String getMacAddress(){
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            if (networkInterfaces == null) {
                return null;
            }

            // 遍历所有网络接口
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                // 过滤掉回环接口（例如：lo, eth0, wlan0等）
                if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                    continue;
                }

                // 获取硬件地址（MAC地址）
                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null) {
                    return bytesToHex(mac);
                   // System.out.format("网络接口: %s, MAC地址: %s%n", networkInterface.getName(), bytesToHex(mac));
                }
            }
        } catch (SocketException e) {
            return null;
        }
        return null;
    }


    // 将字节数组转换为十六进制字符串表示的MAC地址
    private static String bytesToHex(byte[] mac) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return sb.toString();
    }

}
